package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    // 分页查询
    @GetMapping("/page")
    public Result pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageBean pageBean = setmealService.getPage(setmealPageQueryDTO);

        return Result.success(pageBean);

    }

    // 根据id查询套餐
    @GetMapping("/{id}")
    public Result getSetmealById(@PathVariable String id) {
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    // 修改套餐

    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    // 新增套餐
    @PostMapping
    @Cacheable(cacheNames = "setmealCache", key = "'setmeal_'+ #a0.categoryId")
    @ApiOperation("添加套餐")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    //套餐起售、停售
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable String status, String id) {

        setmealService.changeStatus(status, id);
        return Result.success();
    }

    // 批量删除套餐
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @DeleteMapping
    public Result deleteSetmeals(@RequestParam List<String> ids) {
        setmealService.deleteSetmeals(ids);
        return Result.success();
    }


}
